class Solution31 {
    public static boolean[][] check;
    public static int count;
    static int[] dx = {1,-1,0,0};
    static int[] dy = {0,0,1,-1};
    public static int maxAreaOfIsland(int[][] grid) {
        check = new boolean[grid.length][grid[0].length];
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == 1 && check[i][j] == false) {
                    check[i][j] = true;
                    dfs(grid,i,j,1);
                    check[i][j] = false;
                }
            }
        }
        return count;
    }

    public static void dfs(int[][] grid,int x,int y,int tmp) {
        if(count < tmp) count = tmp;
        for(int k = 0; k < 4; k++) {
            int x1 = x + dx[k];
            int y1 = y + dy[k];
            if(x1 >= 0 && x1 < grid.length && y1 >= 0 && y1 < grid[0].length && grid[x1][y1] == 1 && !check[x1][y1]) {
                check[x1][y1] = true;
                tmp++;
                dfs(grid,x1,y1,tmp++);
                check[x1][y1] = false;
                tmp--;
            }
        }
    }

    public static void main(String[] args) {
        int[][] ret = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
        int count = maxAreaOfIsland(ret);
        System.out.println(count);
    }
}